package com.ruoyi.reportsnhnt.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.syrw.annotation.FIllBasicInfo;
import com.ruoyi.reportsnhnt.domain.ReportSnhntKsxsyBaDetail;
import com.ruoyi.reportsnhnt.mapper.ReportSnhntKsxsyBaDetailMapper;
import com.ruoyi.reportsnhnt.mapper.ReportSnhntKsxsysjBaDetailMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.reportsnhnt.domain.bo.ReportSnhntKsxsyBsInfoBo;
import com.ruoyi.reportsnhnt.domain.vo.ReportSnhntKsxsyBsInfoVo;
import com.ruoyi.reportsnhnt.domain.ReportSnhntKsxsyBsInfo;
import com.ruoyi.reportsnhnt.mapper.ReportSnhntKsxsyBsInfoMapper;
import com.ruoyi.reportsnhnt.service.IReportSnhntKsxsyBsInfoService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 水泥混凝土抗渗性试验检测记录Service业务层处理
 *
 * @author zxs
 * @date 2024-06-21
 */
@RequiredArgsConstructor
@Service
public class ReportSnhntKsxsyBsInfoServiceImpl implements IReportSnhntKsxsyBsInfoService {

    private final ReportSnhntKsxsyBsInfoMapper baseMapper;
    private final ReportSnhntKsxsysjBaDetailMapper sjBaDetailMapper;
    private final ReportSnhntKsxsyBaDetailMapper detailMapper;

    /**
     * 查询水泥混凝土抗渗性试验检测记录
     */
    @Override
    @FIllBasicInfo
    public ReportSnhntKsxsyBsInfoVo queryById(Long excelId) {
        ReportSnhntKsxsyBsInfoVo vo = baseMapper.selectVoById(excelId);
        //查询试件试件子表
//        LambdaQueryWrapper<ReportSnhntKsxsysjBaDetail> sjWrapper = new LambdaQueryWrapper<>();
//        sjWrapper.eq(ReportSnhntKsxsysjBaDetail::getExcelId,excelId);
//        List<ReportSnhntKsxsysjBaDetail> sjVos = sjBaDetailMapper.selectList(sjWrapper);
//        vo.setSjVos(sjVos);
        //查询子表
        LambdaQueryWrapper<ReportSnhntKsxsyBaDetail> detailWrapper = new LambdaQueryWrapper<>();
        detailWrapper.eq(ReportSnhntKsxsyBaDetail::getExcelId, excelId);
        List<ReportSnhntKsxsyBaDetail> detailVos = detailMapper.selectList(detailWrapper);
        if (CollUtil.isNotEmpty(detailVos)) {
            vo.setDetailVos(detailVos);
        }
        return vo;
    }

    /**
     * 查询水泥混凝土抗渗性试验检测记录列表
     */
    @Override
    public TableDataInfo<ReportSnhntKsxsyBsInfoVo> queryPageList(ReportSnhntKsxsyBsInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ReportSnhntKsxsyBsInfo> lqw = buildQueryWrapper(bo);
        Page<ReportSnhntKsxsyBsInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询水泥混凝土抗渗性试验检测记录列表
     */
    @Override
    public List<ReportSnhntKsxsyBsInfoVo> queryList(ReportSnhntKsxsyBsInfoBo bo) {
        LambdaQueryWrapper<ReportSnhntKsxsyBsInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ReportSnhntKsxsyBsInfo> buildQueryWrapper(ReportSnhntKsxsyBsInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ReportSnhntKsxsyBsInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getPage() != null, ReportSnhntKsxsyBsInfo::getPage, bo.getPage());
        lqw.eq(bo.getTotalPage() != null, ReportSnhntKsxsyBsInfo::getTotalPage, bo.getTotalPage());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancdwmc()), ReportSnhntKsxsyBsInfo::getJiancdwmc, bo.getJiancdwmc());
        lqw.eq(StringUtils.isNotBlank(bo.getJilbh()), ReportSnhntKsxsyBsInfo::getJilbh, bo.getJilbh());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcmc()), ReportSnhntKsxsyBsInfo::getGongcmc, bo.getGongcmc());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcbwyt()), ReportSnhntKsxsyBsInfo::getGongcbwyt, bo.getGongcbwyt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpxx()), ReportSnhntKsxsyBsInfo::getYangpxx, bo.getYangpxx());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpmc()), ReportSnhntKsxsyBsInfo::getYangpmc, bo.getYangpmc());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpbh()), ReportSnhntKsxsyBsInfo::getYangpbh, bo.getYangpbh());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsl()), ReportSnhntKsxsyBsInfo::getYangpsl, bo.getYangpsl());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpzt()), ReportSnhntKsxsyBsInfo::getYangpzt, bo.getYangpzt());
        lqw.eq(bo.getLaiysj() != null, ReportSnhntKsxsyBsInfo::getLaiysj, bo.getLaiysj());
        lqw.eq(bo.getShiyjcrq() != null, ReportSnhntKsxsyBsInfo::getShiyjcrq, bo.getShiyjcrq());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytj()), ReportSnhntKsxsyBsInfo::getShiytj, bo.getShiytj());
        lqw.eq(StringUtils.isNotBlank(bo.getWend()), ReportSnhntKsxsyBsInfo::getWend, bo.getWend());
        lqw.eq(StringUtils.isNotBlank(bo.getXiangdsd()), ReportSnhntKsxsyBsInfo::getXiangdsd, bo.getXiangdsd());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyj()), ReportSnhntKsxsyBsInfo::getJiancyj, bo.getJiancyj());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyj()), ReportSnhntKsxsyBsInfo::getPandyj, bo.getPandyj());
        lqw.eq(StringUtils.isNotBlank(bo.getZhuyyqsbmcjbh()), ReportSnhntKsxsyBsInfo::getZhuyyqsbmcjbh, bo.getZhuyyqsbmcjbh());
        lqw.eq(StringUtils.isNotBlank(bo.getShebIds()), ReportSnhntKsxsyBsInfo::getShebIds, bo.getShebIds());
        lqw.eq(StringUtils.isNotBlank(bo.getHunntphb()), ReportSnhntKsxsyBsInfo::getHunntphb, bo.getHunntphb());
        lqw.eq(StringUtils.isNotBlank(bo.getShejksdj()), ReportSnhntKsxsyBsInfo::getShejksdj, bo.getShejksdj());
        lqw.eq(StringUtils.isNotBlank(bo.getHunntsjqddj()), ReportSnhntKsxsyBsInfo::getHunntsjqddj, bo.getHunntsjqddj());
        lqw.eq(bo.getChengxrq() != null, ReportSnhntKsxsyBsInfo::getChengxrq, bo.getChengxrq());
        lqw.eq(StringUtils.isNotBlank(bo.getLingq()), ReportSnhntKsxsyBsInfo::getLingq, bo.getLingq());
        lqw.eq(StringUtils.isNotBlank(bo.getHunntzl()), ReportSnhntKsxsyBsInfo::getHunntzl, bo.getHunntzl());
        lqw.eq(StringUtils.isNotBlank(bo.getYanghtj()), ReportSnhntKsxsyBsInfo::getYanghtj, bo.getYanghtj());
        lqw.eq(StringUtils.isNotBlank(bo.getJianctzsdsy()), ReportSnhntKsxsyBsInfo::getJianctzsdsy, bo.getJianctzsdsy());
        lqw.eq(bo.getJianctzssssjdgs() != null, ReportSnhntKsxsyBsInfo::getJianctzssssjdgs, bo.getJianctzssssjdgs());
        lqw.eq(StringUtils.isNotBlank(bo.getShicksdj()), ReportSnhntKsxsyBsInfo::getShicksdj, bo.getShicksdj());
        lqw.eq(StringUtils.isNotBlank(bo.getFujsm()), ReportSnhntKsxsyBsInfo::getFujsm, bo.getFujsm());
        lqw.eq(StringUtils.isNotBlank(bo.getJianc()), ReportSnhntKsxsyBsInfo::getJianc, bo.getJianc());
        lqw.eq(StringUtils.isNotBlank(bo.getJil()), ReportSnhntKsxsyBsInfo::getJil, bo.getJil());
        lqw.eq(StringUtils.isNotBlank(bo.getFuh()), ReportSnhntKsxsyBsInfo::getFuh, bo.getFuh());
        lqw.eq(bo.getRiq() != null, ReportSnhntKsxsyBsInfo::getRiq, bo.getRiq());
        lqw.eq(bo.getHanslpjz() != null, ReportSnhntKsxsyBsInfo::getHanslpjz, bo.getHanslpjz());
        return lqw;
    }

    /**
     * 新增水泥混凝土抗渗性试验检测记录
     */
    @Override
    @DSTransactional
    public Boolean insertByBo(ReportSnhntKsxsyBsInfoBo bo) {
        ReportSnhntKsxsyBsInfo add = BeanUtil.toBean(bo, ReportSnhntKsxsyBsInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setExcelId(add.getExcelId());
            //插入试件子表
//            List<ReportSnhntKsxsysjBaDetail> sjDetails = new ArrayList<>();
//            for (int i = 0; i < 6; i++) {
//                ReportSnhntKsxsysjBaDetail sjDetail = new ReportSnhntKsxsysjBaDetail();
//                sjDetail.setExcelId(bo.getExcelId());
//                sjDetails.add(sjDetail);
//            }
//            sjBaDetailMapper.insertBatch(sjDetails);

            List<ReportSnhntKsxsyBaDetail> baDetails = new ArrayList<>();
            for (int j = 0; j < 10; j++) {
                ReportSnhntKsxsyBaDetail baDetail = new ReportSnhntKsxsyBaDetail();
                baDetail.setExcelId(bo.getExcelId());
                baDetails.add(baDetail);
            }
            detailMapper.insertBatch(baDetails);
        }
        return flag;
    }

    /**
     * 修改水泥混凝土抗渗性试验检测记录
     */
    @Override
    @DSTransactional
    public Boolean updateByBo(ReportSnhntKsxsyBsInfoBo bo) {
        ReportSnhntKsxsyBsInfo update = BeanUtil.toBean(bo, ReportSnhntKsxsyBsInfo.class);
        validEntityBeforeSave(update);
        //更新试件子表
//        List<ReportSnhntKsxsysjBaDetail> sjVos = bo.getSjVos();
//        if(sjVos!=null&&sjVos.size()!=0){
//            sjBaDetailMapper.updateBatchById(sjVos);
//        }
        //更新子表
        List<ReportSnhntKsxsyBaDetail> detailVos = bo.getDetailVos();
        if (detailVos != null && detailVos.size() != 0) {
            detailMapper.updateBatchById(detailVos);
        }
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ReportSnhntKsxsyBsInfo entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除水泥混凝土抗渗性试验检测记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
